import React, {Component} from "react";
import {inject, observer} from "mobx-react";
import {observable, action} from "mobx";

@inject("activityStore")
@observer
class MainLaunchActivity extends Component {
    constructor(props) {
        super(props);
        this.activityModel = this.props.store.getModel();
    }

    @action handleChange = e => {
        switch (e.target.name) {
            case "leader" :
                this.activityModel.leader = e.target.value;
                break;
            case "lphone":
                this.activityModel.lphone = e.target.value;
                break;
            case "wechat" :
                this.activityModel.wechat = e.target.value;
                break;
            case "name" :
                this.activityModel.name = e.target.value;
                break;
            case "address":
                this.activityModel.address = e.target.value;
                break;
            case "time":
                this.activityModel.time = e.target.value;
                break;
            case "deadline":
                this.activityModel.deadline = e.target.value;
                break;
            case "participants":
                this.activityModel.participants = e.target.value;
                break;
            case "partnum":
                this.activityModel.partnum = e.target.value;
                break;
            case "plan":
                this.activityModel.plan = e.target.value;
                break;
        }
    };
    isEmpty = (obj) => {
        for (var name in obj) {
            return false;
        }
        return true;
    };
    goBack = () => {
        this.props.store.setType(-1);
    };
    add = () => {
        const {leader, lphone, wechat, name, address, time, deadline, participants, partnum, plan} = this.activityModel;
        if (this.isEmpty(leader)) {
            alert("发起人姓名不能为空");
            return;
        }
        if (this.isEmpty(lphone)) {
            alert("联系电话不能为空");
            return;
        }
        if (this.isEmpty(wechat)) {
            alert("微信号不能为空");
            return;
        }
        if (this.isEmpty(name)) {
            alert("活动名称不能为空");
            return;
        }
        if (this.isEmpty(address)) {
            alert("活动地址不能为空");
            return;
        }
        if (this.isEmpty(time)) {
            alert("活动时间不能为空");
            return;
        }
        if (this.isEmpty(deadline)) {
            alert("活动报名截止日期不能为空");
            return;
        }
        if (this.isEmpty(participants)) {
            alert("活动参与对象不能为空");
            return;
        }
        if (this.isEmpty(partnum)) {
            alert("活动参与人数不能为空");
            return;
        }
        if (this.isEmpty(plan)) {
            alert("活动简介不能为空");
            return;
        }
        this.props.store.setModel(this.activityModel);
        this.props.store.launchActivity(this.activityModel);
        this.props.store.setType(-1);
    };

    render() {
        const type = this.props.store.getType();
        const {leader, lphone, wechat, name, address, time, deadline, participants, partnum, plan} = this.props.store.activityModel;
        const args = [leader, lphone, wechat, name, address, time, deadline, participants, partnum, plan];
        const names = ["发起人姓名", "联系电话", "微信号", "活动名称", "活动地点", "活动时间", "报名截止时日期", "活动参与对象", "活动参与人数", "活动简介", this.handleChange];
        const temps = ["leader", "lphone", "wechat", "name", "address", "time", "deadline", "participants", "partnum", "plan"];
        return (
            <div>
                {type == 0 ? (<div>普通活动发起信息</div>) : <div>值年活动发起信息</div>}
                <p>请输入您要发起活动的信息</p>
                {args.map(function (item, index) {
                    return (
                        <div>
                            <label>{names[index]}：
                                <input name={temps[index]} type="text" value={item} onChange={names[10]}/>
                            </label>
                        </div>
                    );
                })
                }
                <button onClick={this.add}>发起活动</button>
                <button onClick={this.goBack}>返回</button>
            </div>
        );
    }
}

export default observer(MainLaunchActivity);
